﻿using System;
using ExtraClub.Domain;
using ExtraClub.Domain.Entities;
using SubSonic;
using System.Collections.Generic;

namespace ExtraClub.Web.Controls.poat {
    public partial class module : BaseModule {

        protected Club CurrentClub = null;
        protected List<MailTemplate> list = null;
        protected int SelectedId = 0;
        private bool FormChanged = false;

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

           Page.PreviousPage.FindControl( Body.Config["CustomConfigurationsPath"] = ResolveUrl("fck.config.js");
            Body.ToolbarSet = "PluginTest" ;
            Body.PluginsPath=  ResolveUrl("_plugins") ;

            Query q = ClubInUser.CreateQuery();
            q.AddWhere(ClubInUser.Columns.UserId, CurrentUser.ProviderUserKey);
            ClubInUserCollection club_in_user = new ClubInUserController().FetchByQuery(q);

            if (club_in_user.Count > 0) {
                CurrentClub = club_in_user[0].Club;
                if (!IsPostBack) {
                } else {
                    if (Request.Form["select"] != null) {
                        SelectedId = int.Parse(Request.Form["select"]);
                    }
                }
            }
            if (CurrentClub != null) {
                list = CurrentClub.MailTemplateRecords.GetList();
            }
        }
        protected void delete_Click(object sender, EventArgs e) {
            if (CurrentClub != null && SelectedId > 0) {
                try {
                    MailTemplate.Delete(SelectedId);
                } catch (Exception ex) {
                }
            }
        }
        protected void update_Click(object sender, EventArgs e) {
            if (CurrentClub != null && FormChanged) {
                try {
                    MailTemplate client = MailTemplate.FetchByID(SelectedId);
                    client.Title = Title.Text;
                    client.Description = Description.Text;
                    client.Body = Body.Value;  
                    client.Club = CurrentClub;
                    client.Save();
                } catch (Exception ex) {
                }
            }
        }
        protected void new_Click(object sender, EventArgs e) {
            if (CurrentClub != null) {
                try {
                    MailTemplate client = new MailTemplate();
                    client.Title = Title.Text;
                    client.Description = Description.Text;
                    client.Body = Body.Value;
                    client.Club = CurrentClub;
                    client.Save();
                } catch (Exception ex) {
                }
            }
        }
        protected void form_TextChanged(object sender, EventArgs e) {
            FormChanged = true;
        }
    }
}
